﻿using System;
using System.Windows;
using LFSRecord2.Model;
using LFSRecord2.ViewModel;

//[assembly: CLSCompliant(true)]
namespace LFSRecord2
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);

            MainWindow window = new MainWindow();
            MainWindowViewModel viewModel = new MainWindowViewModel();
            window.DataContext = viewModel;
            window.Show();

            // Load command line args.
            viewModel.LoadProjectFromArgs(e.Args);

            Current.MainWindow.Closing += new System.ComponentModel.CancelEventHandler(MainWindow_Closing);
            Current.MainWindow.Closed += new EventHandler(MainWindow_Closed);
        }

        void MainWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            MainWindowViewModel mainWindowViewModel = (MainWindowViewModel)Current.MainWindow.DataContext;

            if (mainWindowViewModel.CanDestroyProject())
            {
                LfsRecordController.Dispose();
                e.Cancel = false;
            }
            else
            {
                e.Cancel = true;
            }
        }

        void MainWindow_Closed(object sender, EventArgs e)
        {
            // Window is disposed in App by this point, so get from sender instead.
            MainWindowViewModel mainWindowViewModel = (MainWindowViewModel)((MainWindow)sender).DataContext;

            mainWindowViewModel.WindowClosing();
        }
    }
}
